package com.benson.pattern.reactor.impl;

import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;

import com.benson.pattern.reactor.Demultiplexer;
import com.benson.pattern.reactor.EventType;
import com.benson.pattern.reactor.Handler;
import com.benson.pattern.reactor.HandlerEvent;

public class Selector implements Demultiplexer{

	private final int SIZE = 100;
	private BlockingQueue<EventType> _queue = new LinkedBlockingDeque<EventType>(SIZE);
	
	private Map<EventType,Handler> map = new ConcurrentHashMap<EventType,Handler>();
	@Override
	public HandlerEvent select() {
		try {
			EventType type = _queue.take();
			SelectionKey key = new SelectionKey();
			key.setType(type);
			return key;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		throw new RuntimeException(" cannot select any event");
	}

	public void regiest(EventType type ,Handler handler){
		map.put(type, handler);
	}
}
